package com.hzjq.encrypt.ui

import android.content.Context
import android.content.Intent
import com.hzjq.encrypt.R
import com.hzjq.encrypt.adapter.LgAdapter
import com.hzjq.encrypt.bean.EnBean
import com.hzjq.encrypt.bean.Lg
import com.hzjq.encrypt.bean.Lgs
import com.hzjq.encrypt.pop.LgEditorPop

class LgEditorActivity : AbsListActivity<Lg>() {

    companion object {
        fun start(context: Context, data: EnBean?) {
            val intent = Intent(context, LgEditorActivity::class.java)
            if (data == null) {
                intent.putExtra("data", EnBean())
            } else {
                intent.putExtra("data", data)
            }
            context.startActivity(intent)
        }

    }

    private val mLgAdapter = LgAdapter(arrayListOf())

    override fun onDataCreated() {
        mAdapter = this.mLgAdapter

        if (data.lgs == null) {
            data.lgs = Lgs()
        }
        if (data.lgs.lg.isNullOrEmpty()) {
            data.lgs.lg = arrayListOf()
        }

        mLgAdapter.setNewInstance(data.lgs.lg)

        setShowTitle("雷管(${data.lgs.lg.size})发")
    }

    override fun onRemoveItemClick(position: Int) {
        data.lgs.lg.removeAt(position)
        setShowTitle("雷管(${data.lgs.lg.size})发")
        notifyDataChanged()
    }

    override fun onItemEditorClick(position: Int) {
        LgEditorPop.show(this, object : LgEditorPop.OnLgEditorCallback {
            override fun onLgEditor(lg: Lg) {
                data.lgs.lg[lg.position] = lg
                notifyDataChanged()
            }
        }, mLgAdapter.getItem(position))
    }

    override fun onAddNewClick() {
        val lge = Lg()
        lge.position = -1
        LgEditorPop.show(this, object : LgEditorPop.OnLgEditorCallback {
            override fun onLgEditor(lg: Lg) {
                if (data.lgs == null) {
                    data.lgs = Lgs()
                }
                if (data.lgs.lg.isNullOrEmpty()) {
                    data.lgs.lg = arrayListOf()
                }
                data.lgs.lg.add(0, lg)
                mLgAdapter.setNewInstance(data.lgs.lg)
                setShowTitle("雷管(${data.lgs.lg.size})发")
                notifyDataChanged()
            }
        }, lge)
    }


    override fun getContentViewId(): Int {
        return R.layout.activity_lg_list_layout
    }
}